'use strict';

const { Service } = require('egg');

class GradeService extends Service {
  async getList(offset, limit) {
    const { ctx, app } = this;
    const db = app.mysql.get('spring');
    const pageCondition = {
      offset,
    };
    if (limit) {
      pageCondition.limit = limit;
    }
    const condition = Object.assign({}, pageCondition);
    let result = await db.select('t_grade', condition);
    result = ctx.camelcaseKeys(result);
    return result;
  }

  async getCount() {
    const { app } = this;
    const db = app.mysql.get('spring');
    const result = await db.query('select count(*) as count from t_grade');
    return result.length ? result[0].count : 0;
  }

  async create(education) {
    const { app } = this;
    const db = app.mysql.get('spring');
    const { gradeName, gradeType, educationType } = education;
    try {
      const result = await db.insert('t_grade', {
        grade_name: gradeName,
        grade_type: gradeType,
        education_type: educationType,
      });
      return result;
    } catch (e) {
      return e;
    }
  }
}

module.exports = GradeService;
